//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#include "WeCorePrerequisites.h"
#include "WeBaseApplication.h"
#include "WeApplication.h"
#include "WeInputEvents.h"
#include "WePlatform.h"
#include "WeInput.h"
#include "WeVirtualInput.h"
#include "WeTime.h"
#include "WeDebug.h"
#include "WeShaderProg.h"
#include "WeVector3.h"
#include "WeMatrix4.h"
#include "WeMath.h"

#include "spaceship.h"

#include "WeImage.h"
#include "WeTexture2D.h"

using namespace WackiEngine;


class MyApplication : public Application
{
public:
	MyApplication()
		: Application(1280, 800, "MyApplication")
	{
		// connect input callbacks
		gInput().keyInput.connect(this, &MyApplication::onKey);
		gInput().characterInput.connect(this, &MyApplication::onCharInput);
		gInput().mouseMove.connect(this, &MyApplication::onMouseMove);
		gInput().mouseDown.connect(this, &MyApplication::onMouseDown);
		gInput().mouseUp.connect(this, &MyApplication::onMouseUp);
		gInput().mouseDoubleClick.connect(this, &MyApplication::onMouseDoubleClick);
		gInput().scrollWheel.connect(this, &MyApplication::onMouseWheel);

		// define axes and actions
		gVirtualInput().addAxis("MoveUp");
		gVirtualInput().addAxisMapping("MoveUp", KC_Gamepad_LeftY);
		gVirtualInput().addAxisMapping("MoveUp", KC_W, 1.0f);
		gVirtualInput().addAxisMapping("MoveUp", KC_S, -1.0f);

		gVirtualInput().addAxis("MoveRight");
		gVirtualInput().addAxisMapping("MoveRight", KC_Gamepad_LeftX);
		gVirtualInput().addAxisMapping("MoveRight", KC_D, 1.0f);
		gVirtualInput().addAxisMapping("MoveRight", KC_A, -1.0f);

		gVirtualInput().addAxis("LookUp");
		gVirtualInput().addAxisMapping("LookUp", KC_Gamepad_RightY);
		gVirtualInput().addAxisMapping("LookUp", KC_UpArrow, 1.0f);
		gVirtualInput().addAxisMapping("LookUp", KC_DownArrow, -1.0f);

		gVirtualInput().addAxis("LookRight");
		gVirtualInput().addAxisMapping("LookRight", KC_Gamepad_RightX);
		gVirtualInput().addAxisMapping("LookRight", KC_RightArrow, 1.0f);
		gVirtualInput().addAxisMapping("LookRight", KC_LeftArrow, -1.0f);

		gVirtualInput().addAction("Shoot");
		gVirtualInput().addActionMapping("Shoot", KC_Mouse_Left);
		gVirtualInput().addActionMapping("Shoot", KC_Gamepad_RightTrigger);

		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		_ship.setupInput();
	}

	void update()
	{
		Application::update();

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		_ship.update();
		_ship.render();

		// temporary fps display to see performance problems
		static float fpsSamples[100] = { 0.0f };
		static int fpsIndex = 0;
		static float fpsSum = 0.0f;

		float testDelta = gTime().getFrameDelta();

		if(testDelta > 0.0f) {

			fpsSum -= fpsSamples[fpsIndex];
			fpsSamples[fpsIndex] = 1.0f / gTime().getFrameDelta();
			fpsSum += fpsSamples[fpsIndex];

			fpsIndex++;
			fpsIndex %= 100;

			_fps = fpsSum;
			_fps *= 0.01f;

			//gPlatform().setTitle(toString(round(_fps), 0));
		}
	}



	void onKey(const KeyEvent& e)
	{
	}

	void onCharInput(const CharInputEvent& e)
	{
	}

	void onMouseMove(const PointerEvent& e)
	{
	}

	void onMouseDown(const PointerEvent& e)
	{
	}

	void onMouseUp(const PointerEvent& e)
	{
	}

	void onMouseDoubleClick(const PointerEvent& e)
	{
	}

	void onMouseWheel(const PointerEvent& e)
	{
	}

private:
	float		_fps;
	SpaceShip	_ship;
};

void main()
{
	MyApplication app;
	app.run();
}